import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/article_item.dart';
import 'package:flutter_syt/domain/widgets/flow_item.dart';
import 'package:flutter_syt/domain/widgets/flow_waterfall_item.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/normal_grid.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class NewsFocusPage extends StatefulWidget {
  static Map<String, NormalTableViewController> controller = {};
  var id = '';
  int showType = 1;
  @override
  State<StatefulWidget> createState() => _NewsFocusPage();
}

class _NewsFocusPage extends State<NewsFocusPage> {
  NormalTableViewController _normalTableViewController =
      NormalTableViewController();
  NormalGridController gridController = NormalGridController();
  List<FlowItemEntity> topArticles = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    NewsFocusPage.controller[widget.id] = _normalTableViewController;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: widget.showType > 1 ? _buildGridView() : _buildNormalTable());
  }

  Widget _buildNormalTable() {
    return NormalTableView<FlowItemEntity>(
      controller: _normalTableViewController,
      itemBuilder: (context, item) {
        return GestureDetector(
            child: FlowItem(item),
            onTap: ClickUtils.debounce(
              () => RouteManager.push(
                context,
                item.linkUrl,
              ),
            ));
      },
      pageSize: (pn) {
        return 15;
      },
      getData: (pageNumber) async {
        // List<ArticleListItem> lists = await API.shared.article
        //     .articleList(pageNumber, alias: widget.id);
        List<ArticleListItem> lists = await API.shared.article
            .articleInformationList(pageNumber, id: widget.id);
        List<FlowItemEntity> allEntities = [];
        lists.forEach((e) {
          allEntities.add(FlowItemEntity.create(
              e.title, e.publish_time, e.thumb, e.linkUrl,
              labelText: e.tag,
              author: e.author,
              width: e.width,
              height: e.height));
        });
        if (pageNumber == 1) {
          topArticles.clear();
        }
        topArticles.addAll(allEntities);
        for (var item in allEntities) {
          item.useEnDate = true;
        }
        return allEntities;
      },
    );
  }

  Widget _buildGridView() {
    return NormalGridView<FlowItemEntity>(
      padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
      crossAxisCount: widget.showType,
      controller: gridController,
      itemBuilder: (ctx, item, index) {
        return FlowWaterfallItem(item);
      },
      pageSize: (pn) {
        return 15;
      },
      getData: (pageNumber) async {
        // List<ArticleListItem> lists = await API.shared.article
        //     .articleList(pageNumber, alias: widget.id);
        List<ArticleListItem> lists = await API.shared.article
            .articleInformationList(pageNumber, id: widget.id);
        List<FlowItemEntity> allEntities = [];
        lists.forEach((e) {
          allEntities.add(FlowItemEntity.create(
              e.title, e.publish_time, e.thumb, e.linkUrl,
              labelText: e.tag,
              author: e.author,
              width: e.width,
              height: e.height));
        });
        if (pageNumber == 1) {
          topArticles.clear();
        }
        topArticles.addAll(allEntities);
        for (var item in allEntities) {
          item.useEnDate = true;
        }
        return allEntities;
      },
    );
  }
}
